package com.qiao.glod.util;

import android.os.Handler;
import android.os.Looper;


/**
 * Created by Qiao on 2016/4/6.
 */
public class DoubclickHandler {
    private static DoubclickHandler mInstatce = new DoubclickHandler();
    public static DoubclickHandler getInstance(){
        return mInstatce;
    }

    private final int RESET_DELAY_TIME = 2000;
    private boolean isReady;
    private Handler mHandler;

    private DoubclickHandler(){
        isReady = false;
        mHandler = new Handler(Looper.getMainLooper());
    }

    public void watch(DoubleConfirmedListener listener){
        if(!isReady){
            isReady = true;
            mHandler.postDelayed(mRestRunnable,RESET_DELAY_TIME);
            if(listener!=null){
                listener.showTips();
            }
        }else{
            isReady = false;
            mHandler.removeCallbacks(mRestRunnable);
            if(listener!=null){
                listener.OnDoubleConfirmed();
            }
        }
    }

    private Runnable mRestRunnable = new Runnable() {
        @Override
        public void run() {
            isReady = false;
        }
    };

    public interface DoubleConfirmedListener{
        void showTips();
        void OnDoubleConfirmed();
    }
}
